using LitJson;
namespace CGF
{
    public partial class PanelEditPassword : UIComponentBase, IAwake<UILayer, bool>, IDestroy
    {
        public enum Type
        {
            LoginPasswd,
            WithdrawPasswd,
        }
        
        private Type type;

        public static void Open(Type type)
        {
            var instance = UIManagerComponent.instance.OpenUI<PanelEditPassword>(UILayer.Normal, RootComponent.instance, true);
            instance.SetProsBindCompletedAction(() =>
            {
                instance.SetInfo(type);
            });
        }

        public static void Close()
        {
            UIManagerComponent.instance.CloseUI<PanelEditPassword>();
        }

        public async void Awake(UILayer layer, bool isFloat)
        {
            await SpawnPrefab("Prefabs/GameUI/PanelEditPassword", layer, isFloat);
            BindProperties(transform);

            BtnClose.SetClick(Close);

            BtnConfirm.SetClick(async () =>
            {
                if (InputPasswdOld.gameObject.activeSelf)
                {
                    if (string.IsNullOrEmpty(InputPasswdOld.text))
                    {
                        CGF.Dialog.Alert(I18N_please_input_pw_old);
                        return;
                    }
                    if (!StringHelper.IsNumberAndLetter(InputPasswdOld.text))
                    {
                        CGF.Dialog.Alert(CommAssets.instance.I18N_passwd_must_letter8);
                        return;
                    }
                }
                if (string.IsNullOrEmpty(InputPasswdNew1.text) || string.IsNullOrEmpty(InputPasswdNew2.text))
                {
                    CGF.Dialog.Alert(I18N_please_input_pw_new);
                    return;
                }
                if (!StringHelper.IsNumberAndLetter(InputPasswdNew1.text))
                {
                    CGF.Dialog.Alert(CommAssets.instance.I18N_passwd_must_letter8);
                    return;
                }
                if (InputPasswdNew1.text != InputPasswdNew2.text)
                {
                    CGF.Dialog.Alert(I18N_pw_not_same);
                    return;
                }
                JsonData data = new JsonData();
                if (type == Type.LoginPasswd)
                {
                    data["old_password"] = InputPasswdOld.text;
                    data["password"] = InputPasswdNew1.text;
                    var res = await Network.Post("/v1/reward/oauth/change", data);
                    if (!res.success)
                    {
                        CGF.Dialog.Alert(CommAssets.instance.I18N_curr_passwd_wrong);
                        return;
                    }
                    CGF.Dialog.Alert(I18N_edit_success, Close);
                }
                else
                {
                    data["old_withdrawal_password"] = InputPasswdOld.text;
                    data["withdrawal_password"] = InputPasswdNew1.text;
                    var res = await Network.Post("/v1/reward/oauth/change", data);
                    if (res.success)
                    {
                        JsonData info = UserInfo.Info;
                        info["withdrawal_password"] = "******";
                        UserInfo.Info = info;
                        EventManager.Emit(new Event_UserInfoUpdate());
                        CGF.Dialog.Alert(I18N_edit_success, Close);
                    }
                }
            });

            ScaleIn(Dialog.transform);
        }

        public void SetInfo(Type type)
        {
            this.type = type;
            if (type == Type.LoginPasswd)
            {
                InputPasswdOld.gameObject.SetActive(true);
                Title.text = I18N_modify_login_pw;
            }
            else
            {
                string oldPw = UserInfo.Info["withdrawal_password"].ToString();
                InputPasswdOld.gameObject.SetActive(!string.IsNullOrEmpty(oldPw));
                Title.text = InputPasswdOld.gameObject.activeSelf ? I18N_modify_withdr_pw : I18N_set_withdr_pw;
            }
        }

        public override void OnDestroy()
        {
            base.OnDestroy();
        }

    }
}
